﻿using System;
using System.Web;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Xml.Linq;
using System.Collections;
using System.Web.Security;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BLL;
using DLL;

public partial class xsPage : System.Web.UI.Page
{
    //当前班级编号
    private static string bjNum = "";
    private static string zgId = "";

    // 画面初始化
    protected void Page_Load(object sender, EventArgs e)
    {
        //获取员工登陆信息
        if (Session["landInfo"] != null)
        {
            landInfo info = (landInfo)Session["landInfo"];
            //获取员工编号
            zgId = info.UserId;
        }
        else
        {
            //显示消息
            MsgBoxShow("请重新登陆！");
            //如果员工信息获取失败则跳转到登陆页
            Response.Write("<script type='text/javascript'>location.reload('landing.aspx')</script>");
        }
        //页面是否回传
        if (!IsPostBack)
        {
            if (Request.QueryString["bjNum"] != null)
            {
                //获取当前班级编号
                bjNum = Request.QueryString["bjNum"];
                labBJ.Text = Request.QueryString["bjName"];
            }
            //获取院系信息
            BindYX();
        }
    }

    #region 数据绑定
    /// <summary>
    /// 获取已有学生数据信息
    /// </summary>
    /// <param name="bjNum">班级编号</param>
    private void BindXS()
    {
        gvXS.DataSource = xs_BLL.GetXSSource(bjNum);
        gvXS.DataBind();
    }

    //获取院系数据信息
    private void BindYX()
    {
        dropYX.DataSource = yx_BLL.GetSource();
        dropYX.DataTextField = "yxName";
        dropYX.DataValueField = "yxNum";
        dropYX.DataBind();
        dropYX_SelectedIndexChanged(null, null);
    }

    //根据院系编号获取专业信息
    protected void dropYX_SelectedIndexChanged(object sender, EventArgs e)
    {
        dropZY.DataSource = zy_BLL.GetSource(dropYX.SelectedValue);
        dropZY.DataTextField = "zyName";
        dropZY.DataValueField = "zyNum";
        dropZY.DataBind();
        dropZY_SelectedIndexChanged(sender, e);
    }

    //根据专业编号获取班级信息
    protected void dropZY_SelectedIndexChanged(object sender, EventArgs e)
    {
        //捕获因专业不存在，而根据专业编号查找班级时产生的错误（不处理）
        try
        {
            //清空班级下拉列表数据项
            dropBJ.Items.Clear();
            //重新绑定下拉列表数据项数据
            dropBJ.DataSource = bj_BLL.GetBJSource(zgId, dropZY.SelectedValue);
            dropBJ.DataTextField = "bjName";
            dropBJ.DataValueField = "bjNum";
            dropBJ.DataBind();
            //如果是从其他页面跳入则根据班级编号加载班级学生信息
            if (bjNum != "")
                BindXS();
            else
                dropBJ_SelectedIndexChanged(sender, e);
        }
        catch { }
    }

    //选择班级
    protected void dropBJ_SelectedIndexChanged(object sender, EventArgs e)
    {
        labBJ.Text = dropBJ.SelectedItem.Text;
        bjNum = dropBJ.SelectedValue;
        BindXS();
    }
    #endregion

    //换页
    protected void gvXS_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvXS.PageIndex = e.NewPageIndex;
        BindXS();
    }

    #region 脚本
    /// <summary>
    /// 显示报消息窗口
    /// </summary>
    /// <param name="Msg"></param>
    private void MsgBoxShow(string Msg)
    {
        RegisterStartupScript("Msg", "<script type='text/javascript' language='javascript'>alert('" + Msg + "')</script>");
    }
    /// <summary>
    /// 刷新当前页
    /// </summary>
    private void RefurPage()
    {
        RegisterStartupScript("refur", "<script type='text/javascript'>location.replace(location.href)</script>");
    }
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvXS.Rows.Count == 0)
        {
            RegisterStartupScript("prompt1", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未找到相关数据)'</script>");
        }
    }
    #endregion

    // 删除gvXS中行
    protected void gvXS_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        bool bl = xs_BLL.DeleteXS(gvXS.DataKeys[e.RowIndex][0].ToString());
        if (bl)
        {
            BindXS();
            MsgBoxShow("成功删除信息！");
        }
        else
        {
            MsgBoxShow("删除失败！");
        }
    }
    //提交
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        //创建新的数据对象并赋值
        XS newXS = new XS();
        newXS.xsName = txtName.Text;
        newXS.xsSex = dropSex.SelectedValue == "1" ? true : false;
        newXS.xsId = txtXH.Text;
        newXS.xsIdCard = txtCard.Text;
        newXS.bjNum = decimal.Parse(bjNum);
        //验证用户操作类型
        if (btnRefur.Text == "添加")
        {
            //请求添加数据
            bool bl = xs_BLL.AddNewXS(newXS);
            if (bl)
            {
                MsgBoxShow("添加成功！");
                if (gvXS.Rows.Count == 0)
                    RefurPage();
                else
                {
                    BindXS();
                    btnCancel_Click(sender, e);
                }
                btnCancel_Click(sender, e);
            }
            else
                MsgBoxShow("添加失败！");
        }
        else
        {
            //请求更新数据
            bool b = xs_BLL.UpdateXS(seleXSId, newXS);
            if (b)
            {
                MsgBoxShow("更新成功！");
                BindXS();
                btnCancel_Click(sender, e);
            }
            else
            {
                MsgBoxShow("更新失败！");
                btnCancel_Click(sender, e);
            }
        }
    }
    //取消
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtCard.Text = txtName.Text = txtXH.Text = "";
        dropSex.ClearSelection();
        btnRefur.Text = "添加";
    }

    //选中行
    private static string seleXSId = "";
    protected void gvXS_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //获取选中学生学号
        seleXSId = gvXS.DataKeys[e.NewSelectedIndex][0].ToString();
        //获取当前选中行
        GridViewRow row = gvXS.Rows[e.NewSelectedIndex];
        //填充控件
        txtXH.Text = row.Cells[1].Text;
        txtName.Text = row.Cells[2].Text;
        dropSex.ClearSelection();
        dropSex.Items.FindByText(row.Cells[3].Text).Selected = true;
        txtCard.Text = row.Cells[4].Text;
        btnRefur.Text = "更新";
    }
}
